﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Order_Management
{
    class OrderService
    {
        public OrderService()
        {
            this.orderNum = 0;
            this.orderList = new List<Order>();
        }

        private int orderNum;
        public List<Order> orderList;

        public int GetNum()
        {
            return this.orderNum;
        }

        public void AddOrder(Order newOrder)
        {
            this.orderList.Add(newOrder);
            this.orderNum++;
        }

        public void DeleteOrder(int id)
        {
            int index = 0;
            bool isFind = false;
            foreach (Order n in this.orderList)
            {
                if (n.id == id)
                {
                    isFind = true;
                    index = this.orderList.IndexOf(n);
                }
            }
            if (isFind)
            {
                this.orderList.RemoveAt(index);
                this.orderNum--;
                Console.WriteLine("订单删除成功");
            }
            else
            {
                Console.WriteLine("未找到相关订单");
            }
        }

        public void ModifyOrder(int id)
        {
            int index = 0;
            bool isFind = false;
            foreach (Order n in this.orderList)
            {
                if (id == n.id)
                {
                    isFind = true;
                    index = this.orderList.IndexOf(n);
                }
            }
            if (isFind)
            {
                int choice;
                Console.WriteLine("请选择您要修改的数据项：");
                Console.WriteLine("（1）订单号");
                Console.WriteLine("（2）客户名");
                Console.WriteLine("（3）订单金额");
                Console.WriteLine("（4）商品信息");
                Console.WriteLine("（5）增加商品");
                Console.WriteLine("（6）删除商品");
                Console.WriteLine("请输入对应数字选择功能：");
                choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        {
                            int newId;
                            Console.WriteLine("请输入新的订单号");
                            newId = Convert.ToInt32(Console.ReadLine());
                            this.orderList[index].id = newId;
                            Console.WriteLine("订单修改成功");
                            break;
                        }
                    case 2:
                        {
                            string newName;
                            Console.WriteLine("请输入新的客户名");
                            newName = Console.ReadLine();
                            this.orderList[index].orderDetails.SetCustomer(newName);
                            Console.WriteLine("订单修改成功");
                            break;
                        }
                    case 3:
                        {
                            int newAmount;
                            Console.WriteLine("请输入新的订单金额");
                            newAmount = Convert.ToInt32(Console.ReadLine());
                            this.orderList[index].orderDetails.amount = newAmount;
                            Console.WriteLine("订单修改成功");
                            break;
                        }
                    case 4:
                        {
                            Console.WriteLine("请输入您要修改的商品名");
                            string name = Console.ReadLine();
                            int goodsIndex = 0;
                            bool goodsIsFind = false;
                            foreach (Goods n in this.orderList[index].orderDetails.goodsList)
                            {
                                if (n.name == name)
                                {
                                    goodsIsFind = true;
                                    goodsIndex = this.orderList[index].orderDetails.goodsList.IndexOf(n);
                                }
                            }
                            if (!goodsIsFind)
                            {
                                Console.WriteLine("未找到相关商品");
                            }
                            int flag;
                            Console.WriteLine("请输入您要改变的商品信息");
                            Console.WriteLine("（1）商品名");
                            Console.WriteLine("（2）商品数目");
                            Console.WriteLine("（3）商品价格");
                            flag = Convert.ToInt32(Console.ReadLine());
                            switch (flag)
                            {
                                case 1:
                                    {
                                        string newName;
                                        Console.WriteLine("请输入新的商品名");
                                        newName = Console.ReadLine();
                                        this.orderList[index].orderDetails.goodsList[goodsIndex].name = newName;
                                        Console.WriteLine("订单修改成功");
                                        break;
                                    }
                                case 2:
                                    {
                                        Console.WriteLine("请输入新的商品数目");
                                        int newNum = Convert.ToInt32(Console.ReadLine());
                                        this.orderList[index].orderDetails.goodsList[goodsIndex].number = newNum;
                                        this.orderList[index].orderDetails.UpdateAmount();
                                        Console.WriteLine("订单修改成功");
                                        break;
                                    }
                                case 3:
                                    {
                                        Console.WriteLine("请输入新的商品价格");
                                        int newPrice = Convert.ToInt32(Console.ReadLine());
                                        this.orderList[index].orderDetails.goodsList[goodsIndex].price = newPrice;
                                        this.orderList[index].orderDetails.UpdateAmount();
                                        Console.WriteLine("订单修改成功");
                                        break;
                                    }
                                default:
                                    Console.WriteLine("输入错误，请重新输入！");
                                    break;
                            }
                            break;
                        }
                    case 5:
                        {
                            Console.WriteLine("商品的名字是：");
                            string goodsName = Console.ReadLine();
                            Console.WriteLine("第商品的数量是：");
                            int goodsNumber = Convert.ToInt32(Console.ReadLine());
                            Console.WriteLine("第商品的价格是：");
                            int goodsPrice = Convert.ToInt32(Console.ReadLine());
                            Goods goods = new Goods(goodsName, goodsNumber, goodsPrice);
                            this.orderList[index].orderDetails.AddGoods(goods);
                            break;
                        }
                    case 6:
                        {
                            Console.WriteLine("请输入要删除的商品的名字：");
                            string goodsName = Console.ReadLine();
                            this.orderList[index].orderDetails.DeleteGoods(goodsName);
                            break;
                        }
                    default:
                        Console.WriteLine("输入错误，请重新输入！");
                        break;
                }
            }
            else
            {
                Console.WriteLine("未找到该订单号的订单");
            }
        }
    }
}
